﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using STHPMS1.BLL.B_Core;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using STHPMS1.ICore.I_Serv;




namespace STHPMS1.BLL.B_Bus
{
    public class Df_Main_CostBus : BllBase, IDf_Main_CostBill
    {
        public void Save<T>(T entity)
            where T : class ,IDf_Main_Cost
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IDf_Main_Cost entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IDf_Main_Cost
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }


        /// <summary>
        /// 资金不受限 资金分配
        /// </summary>
        /// <param name="anniname"></param>
        /// <param name="analy_year"></param>
        public void Insert(string anniname, string analy_year)
        {
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();

            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            dao.Delete<Df_Main_Cost>(p => p.Anni_Name == anniname && p.Analy_Year == analy_year);  //资金模型名称


            Expression<Func<Df_Costmod_Para, bool>> exp = null;
            if (anniname != null && anniname.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anniname.ToString() && pre.Analy_Year == analy_year;

            List<Df_Costmod_Para> data = this.Scene.Bll.Df_Costmod_Para.Get<Df_Costmod_Para>(exp, null).ToList(); //资金模型参数  理论上只有一条数据


            //int i = 0;

            foreach (Df_Costmod_Para info in data)
            {
                List<Dn_Divi_Planning> list = dao.Df_Main_Cost.GetDivi<Dn_Divi_Planning>(info.Anni_Name, info.Analy_Year);

                foreach (Dn_Divi_Planning diviinfo in list)
                {
                    Df_Main_Cost maininfo = new Df_Main_Cost();
                    maininfo.Id = serv.NewGuid(); //Id
                    maininfo.Anni_Name = diviinfo.Anni_Name; //资金模型名称
                    maininfo.Roadnet_Name = info.Roadnet_Name; //路网名称
                    maininfo.Rdwayid = diviinfo.Rdwayid; //路线ID
                    maininfo.Mainsegid = diviinfo.Mainsegid; //决策路段
                    maininfo.Beginmp = diviinfo.Beginmp; //起点桩号
                    maininfo.Endmp = diviinfo.Endmp; //终点桩号
                    maininfo.Segid_Main = diviinfo.Segid_Main; //养护类型 
                    maininfo.Main_Meas = diviinfo.Project_Name; //养护方案
                    maininfo.Carry_Year = diviinfo.Main_Year; //实施年度
                    maininfo.Segid_Maincost = diviinfo.Cost; //决策路段养护费用
                    maininfo.Analy_Year = diviinfo.Divi_Time; //分析年度



                    #region 原有代码：待删除
                    /*
                    //该路段为大中修养护，当年养护后，以后每年则进行日常性养护
                    if ((i >= 1 && diviinfo.Segid_Main == 12002) || (i >= 1 && diviinfo.Segid_Main == 12003))
                    {
                        maininfo.Segid_Main = 12000;
                    }
                    maininfo.Main_Meas = "";
                    maininfo.Carry_Year = info.Analy_Year;
                    maininfo.Analy_Year = info.Analy_Year;

                    if (diviinfo.Segid_Main == 12000) //该路段为日常养护
                    {
                        maininfo.Segid_Main = 12000;
                    }
                    if (diviinfo.Segid_Main == 12001)   //该路段为预防性养护，判断是否单年养护，如果不是则进行日常性养护
                    {
                        string mainyear = dao.Dp_Preven_Detail.GetMainYear(diviinfo.Mainsegid);
                        if (info.Analy_Year == mainyear)
                        {

                            maininfo.Segid_Main = 12001;
                        }
                        else
                        {
                            maininfo.Segid_Main = 12000;
                        }
                    }
                    if ((i == 0 && diviinfo.Segid_Main == 12002) || (i == 0 && diviinfo.Segid_Main == 12003))  //该路段为大中修
                    {
                        maininfo.Segid_Main = diviinfo.Segid_Main;
                    }


                    if (maininfo.Segid_Main == 12000)  //计算日常养护费用
                    {
                        double lenght = Convert.ToDouble(diviinfo.Endmp - diviinfo.Beginmp);
                        //通过决策路段获取基础路段信息
                        IB_Invntory b_invntoryInfo = this.Scene.Bll.Dp_Dailymain_Cost.GetB_Invntory(diviinfo.Mainsegid);
                        int Year = (DateTime.Now - Convert.ToDateTime(b_invntoryInfo.Begindate)).Days / 365;

                        double cost = dao.Df_Dailymian_Cost.GetDaily_Unitcost(Convert.ToInt32(b_invntoryInfo.Pvmttype_Dictid), b_invntoryInfo.Rdclass_Dictid.ToString(), Year);  //通车年限？？？？
                        maininfo.Segid_Maincost = lenght / 1000 * cost;
                    }
                    if (maininfo.Segid_Main == 12001) //计算预防性养护费用
                    {
                        maininfo.Segid_Maincost = dao.Dp_Preven_Detail.GetCost(diviinfo.Mainsegid);
                    }
                    if (maininfo.Segid_Main == 12002) //计算中修费用
                    {
                        maininfo.Segid_Maincost = 10;   //??
                    }
                    if (maininfo.Segid_Main == 12003) //计算大修费用
                    {
                        maininfo.Segid_Maincost = 100;  //??
                    }
                     */
                    #endregion

                    dao.Save<Df_Main_Cost>(maininfo);
                    
                    //更新 路网决策路段表 方案资金,养护年份
                    dao.Update<Dn_Divi_Planning>(new { MainCost = diviinfo.Cost }, p => p.Id == diviinfo.Id);
                }

               // i++;
            }
        }

        /// <summary>
        /// 获取路网的所有决策路段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="roadnetname">路网名称</param>
        /// <returns></returns>
        public List<T> GetDivi<T>(string roadnetname) where T : class,IDn_Divi_Planning
        {
            return this.Scene.Dal.Sthpms1.Df_Main_Cost.GetDivi<T>(roadnetname);
        }

         /// <summary>
        /// 获取路网的所有决策路段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="roadnetname">路网名称</param>
        /// <returns></returns>
        public List<T> GetDivi<T>(string anniname, string Analy_Year) where T : class,IDn_Divi_Planning
        {
            //string SqlSerach = this.Scene.Bll.B_Roadnet.GetSearchSQL(roadnetname);
            return this.Scene.Dal.Sthpms1.Df_Main_Cost.GetDivi<T>(anniname, Analy_Year);
        }
    }
}
